package com.yuanluludev.lib_pic.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.view.View;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.recyclerview.widget.RecyclerView.ItemDecoration;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;
import androidx.recyclerview.widget.RecyclerView.LayoutParams;
import androidx.recyclerview.widget.RecyclerView.State;

public class DividerGridItemDecoration extends ItemDecoration {
    private static final int[] ATTRS = new int[]{16843284};
    private Drawable mDivider;
    private Paint mPaint;
    private int mDividerHeight = 1;
    private int mDividerWidth = 1;
    private int mOrientation;
    private boolean isShowLastLine = true;

    public DividerGridItemDecoration(Context context, int orientation, int dividerWidth, int dividerHeight, int dividerColor) {
        TypedArray a = context.obtainStyledAttributes(ATTRS);
        this.mOrientation = orientation;
        this.mDivider = a.getDrawable(0);
        this.mDividerHeight = dividerHeight;
        this.mDividerWidth = dividerWidth;
        this.mPaint = new Paint(1);
        this.mPaint.setColor(dividerColor);
        this.mPaint.setStyle(Style.FILL);
        a.recycle();
    }

    public DividerGridItemDecoration(Context context, int orientation, int dividerWidth, int dividerHeight, int dividerColor, boolean isShowLastLine) {
        TypedArray a = context.obtainStyledAttributes(ATTRS);
        this.mOrientation = orientation;
        this.mDivider = a.getDrawable(0);
        this.mDividerHeight = dividerHeight;
        this.mDividerWidth = dividerWidth;
        this.mPaint = new Paint(1);
        this.mPaint.setColor(dividerColor);
        this.mPaint.setStyle(Style.FILL);
        this.isShowLastLine = isShowLastLine;
        a.recycle();
    }

    public void onDraw(Canvas c, RecyclerView parent, State state) {
        if (this.mOrientation == 1) {
            this.drawVertical(c, parent);
        } else if (this.mOrientation == 0) {
            this.drawHorizontal(c, parent);
        } else {
            this.drawHorizontal(c, parent);
            this.drawVertical(c, parent);
        }

    }

    private int getSpanCount(RecyclerView parent) {
        int spanCount = -1;
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager)layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager)layoutManager).getSpanCount();
        }

        return spanCount;
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        int childCount;
        if (this.isShowLastLine) {
            childCount = parent.getChildCount();
        } else {
            childCount = parent.getChildCount() - 1;
        }
        for(int i = 0; i < childCount; ++i) {
            View child = parent.getChildAt(i);
            LayoutParams params = (LayoutParams)child.getLayoutParams();
            int left = child.getLeft() - params.leftMargin;
            int right = child.getRight() + params.rightMargin + this.mDivider.getIntrinsicWidth();
            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + this.mDivider.getIntrinsicHeight();
            this.mDivider.setBounds(left, top, right, bottom);
            this.mDivider.draw(c);
            if (this.mPaint != null) {
                c.drawRect((float)left, (float)top, (float)right, (float)bottom, this.mPaint);
            }
        }

    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        int childCount;
        if (this.isShowLastLine) {
            childCount = parent.getChildCount();
        } else {
            childCount = parent.getChildCount() - 1;
        }

        for(int i = 0; i < childCount; ++i) {
            View child = parent.getChildAt(i);
            LayoutParams params = (LayoutParams)child.getLayoutParams();
            int top = child.getTop() - params.topMargin;
            int bottom = child.getBottom() + params.bottomMargin;
            int left = child.getRight() + params.rightMargin;
            int right = left + this.mDivider.getIntrinsicWidth();
            this.mDivider.setBounds(left, top, right, bottom);
            this.mDivider.draw(c);
            if (this.mPaint != null) {
                c.drawRect((float)left, (float)top, (float)right, (float)bottom, this.mPaint);
            }
        }

    }

    private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) {
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos + 1) % spanCount == 0) {
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager)layoutManager).getOrientation();
            if (orientation == 1) {
                if ((pos + 1) % spanCount == 0) {
                    return true;
                }
            } else {
                childCount -= childCount % spanCount;
                if (pos >= childCount) {
                    return true;
                }
            }
        }

        return false;
    }

    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            childCount -= childCount % spanCount;
            if (pos >= childCount) {
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager)layoutManager).getOrientation();
            if (orientation == 1) {
                childCount -= childCount % spanCount;
                if (pos >= childCount) {
                    return true;
                }
            } else if ((pos + 1) % spanCount == 0) {
                return true;
            }
        }

        return false;
    }

    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
        int spanCount = this.getSpanCount(parent);
        int childCount = parent.getAdapter().getItemCount();
        if (this.isLastRaw(parent, itemPosition, spanCount, childCount)) {
            if (this.mOrientation == 1) {
                outRect.set(0, 0, this.mDividerWidth, 0);
            } else if (this.mOrientation == 0) {
                outRect.set(0, 0, 0, this.mDividerHeight);
            } else {
                outRect.set(0, 0, this.mDividerWidth, this.mDividerHeight);
            }
        } else if (this.isLastColum(parent, itemPosition, spanCount, childCount)) {
            if (this.mOrientation == 1) {
                outRect.set(0, 0, this.mDividerWidth, 0);
            } else if (this.mOrientation == 0) {
                outRect.set(0, 0, 0, this.mDividerHeight);
            } else {
                outRect.set(0, 0, this.mDividerWidth, this.mDividerHeight);
            }
        } else if (this.mOrientation == 1) {
            outRect.set(0, 0, this.mDividerWidth, 0);
        } else if (this.mOrientation == 0) {
            outRect.set(0, 0, 0, this.mDividerHeight);
        } else {
            outRect.set(0, 0, this.mDividerWidth, this.mDividerHeight);
        }

    }
}

